package thread.framepackage;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;

/**
 * 信号量 用来控制并发线程个数
 * acquire获取许可证，release释放许可证
 */
public class SemaphoreT {
    private static final ExecutorService pool = Executors.newFixedThreadPool(30);

    private static final Semaphore s = new Semaphore(10);

    public static void main(String[] args) {
        for (int i = 0; i < 30; i++) {
            pool.execute(() -> {
                try {
                    s.acquire();
                    Thread.sleep(1000);
                    System.out.println(Thread.currentThread().getName() + " save data");
                    s.release();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }
    }
}
